<?php
/*
Plugin Name: Diggable
Plugin URI: http://code.google.com/p/diggable/
Description: Add Digg This Button(s) to your website.
Version: 0.2
Author: Logan Lindquist
Author URI: http://llbbl.com
*/


/*
Copyright 2006 Logan Lindquist (logan.lindquist@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/





function diggable_html($display=Array()) {
	$buttonsize = 	get_option('diggable_buttonsize');


	$html = "";

//	$imagepath = get_bloginfo('wpurl') . '/wp-content/plugins/diggable/images/';



	// Load the post's data
/*
	$blogname = urlencode(get_bloginfo('wpurl'));
	global $wp_query; 
	$post = $wp_query->post;
	$permalink = get_permalink($post->ID);
	$title = urlencode($post->post_title);
*/
	if ($buttonsize == "standard"){
		$html .= '<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>';
	}
	
	if ($buttonsize == "compact"){
		$html .= '<script type="text/javascript">'
 					. ' digg_skin = "compact";</script>'
 					. ' <script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>';
	}	

	return $html;
}

// Hook the_content to output html if we should display on any page
$diggable_contitionals = get_option('diggable_conditionals');
if (is_array($diggable_contitionals) and in_array(true, $diggable_contitionals)) {
	add_filter('the_content', 'diggable_display_hook');
	add_filter('the_excerpt', 'diggable_display_hook');
	
	function diggable_display_hook($content='') {
		$conditionals = get_option('diggable_conditionals');
		if ((is_home() and $conditionals['is_home']) or
		    (is_single() and $conditionals['is_single']) or
		    (is_page() and $conditionals['is_page']) or
		    (is_category() and $conditionals['is_category']) or
		    (is_date() and $conditionals['is_date']) or
		    (is_search() and $conditionals['is_search']) or
	            0)
			$content .= diggable_html();
	
		return $content;
	}
}

// Hook wp_head to add css
add_action('wp_head', 'diggable_wp_head');
function diggable_wp_head() {
	echo '<link rel="stylesheet" type="text/css" media="screen" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/diggable/diggable.css" />';
}

// load wp rss functions for update checking.
require_once(ABSPATH . WPINC . '/rss-functions.php');

// Plugin config/data setup
if (function_exists('register_activation_hook')) {
	// for WP 2
	register_activation_hook(__FILE__, 'diggable_activation_hook');
} else {
	// for WP 1.5, which doesn't have any activation hook
	if (!is_array(get_option('diggable_known_sites')))
		diggable_activation_hook();
}
function diggable_activation_hook() {
	return diggable_restore_config(False);
}

// restore built-in defaults, optionally overwriting existing values
function diggable_restore_config($force=False) {

	// only display on single posts and pages by default
	if ($force or !is_array(get_option('diggable_conditionals')))
		update_option('diggable_conditionals', array(
			'is_home' => False,
			'is_single' => True,
			'is_page' => True,
			'is_category' => False,
			'is_date' => False,
			'is_search' => False,
		));

}

// Hook the admin_menu display to add admin page
add_action('admin_menu', 'diggable_admin_menu');
function diggable_admin_menu() {
	add_submenu_page('options-general.php', 'Diggable', 'Diggable', 8, 'Diggable', 'diggable_submenu');
}

// Admin page header



// The admin page
function diggable_submenu() {

if ($_REQUEST['save']) {

		$conditionals = Array();
		if (!$_REQUEST['conditionals'])
			$_REQUEST['conditionals'] = Array();
		foreach(get_option('diggable_conditionals') as $condition=>$toggled)
			$conditionals[$condition] = array_key_exists($condition, $_REQUEST['conditionals']);
		update_option('diggable_conditionals', $conditionals);

	//	update the button size
	
		if (!$_REQUEST['buttonsize'])
			$_REQUEST['buttonsize'] = "standard";
		update_option('diggable_buttonsize', $_REQUEST['buttonsize']);
}

	$conditionals = get_option('diggable_conditionals');
	$buttonsize = 	get_option('diggable_buttonsize');
	
	if (empty($buttonsize)){
		update_option('diggable_buttonsize', 'standard');
		$buttonsize = 'standard';
	}


	// display options
?>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">

<div class="wrap" id="diggable_options">


<h3>Diggable Options</h3>

<fieldset>
<p>Controls where you want the Digg This button to show up.</p>

<ul style="list-style-type: none">
	<li><input type="checkbox" name="conditionals[is_home]"<?php echo ($conditionals['is_home']) ? " checked" : ""; ?>> Front page of the blog</li>
	<li><input type="checkbox" name="conditionals[is_single]"<?php echo ($conditionals['is_single']) ? " checked" : ""; ?>> Individual blog posts</li>
	<li><input type="checkbox" name="conditionals[is_page]"<?php echo ($conditionals['is_page']) ? " checked" : ""; ?>> Individual WordPress "Pages"</li>
</ul>
</fieldset>


<h3>Display options</h3>

<fieldset>
	<p>Button Size</p>

<ul style="list-style-type: none">
	<li><input type="radio" name="buttonsize" value="standard" <?php echo ($buttonsize == "standard") ? " checked" : ""; ?>> Standard Size</li>
	<li><input type="radio" name="buttonsize" value="compact" <?php echo ($buttonsize == "compact") ? " checked" : ""; ?>> Compact Size</li>
</ul>

</fieldset>

<p class="submit"><input name="save" id="save" tabindex="3" value="Save Changes" type="submit" /></p>


</div>


</form>

<?php
}

?>
